﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using LinqtoCRM.Tests.CRM;
using System.Linq.Expressions;

namespace LinqtoCRM.Tests
{
    [TestFixture]
    public class ExceptionTests : LinqtoCRMTests
    {
        [Test]
        [ExpectedException(typeof(NotSupportedException))]
        public void TestSum()
        {
            var sum = provider.Linq<contact>().Sum(c => c.gendercode.Value);
        }

        [Test]
        [ExpectedException(typeof(NotSupportedException))]
        public void TestGroupBy()
        {
            var sum = from c in provider.Linq<contact>()
                      group c by c.statuscode into g
                      select new { Code = g.Key.name, Count = g.Count() };
            string query = provider.GetQueryText(sum.Expression);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestQueryThrowsExceptionOnNullProvider()
        {
            var c = new Query<contact>(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestQueryThrowsExceptionOnNullExpression()
        {
            var c = new Query<contact>(provider,null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void TestQueryThrowsExceptionOnWrongExpression()
        {
            var c = new Query<contact>(provider, Expression.Equal(Expression.Constant(5), Expression.Constant(5)));
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void TestQueryThrowsExceptionBadKeyPredicate()
        {
            var sum = from c in provider.Linq<contact>()
                      where c.contactid == new Key() { Value = new Guid("4058795b-e391-dd11-8068-0003ff39d7e9") }
                      select new { c.fullname };
            string query = provider.GetQueryText(sum.Expression);
        }
     }
}
